package com.java202411.repository;

import com.java202411.entity.project.ProjectEntity;
import com.java202411.entity.project.ProjectStatus;
import com.java202411.repository.client.ProjectDatabaseClient;
import com.java202411.repository.client.ProjectFileDatabaseClient;
import com.java202411.repository.table.ProjectFileTable;
import com.java202411.repository.table.ProjectTable;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.StreamSupport;

@Repository
@RequiredArgsConstructor
public class ProjectRepository {
    private final ProjectDatabaseClient projectDatabaseClient;
    private final ProjectFileDatabaseClient projectfileDatabaseClient;

    public ProjectEntity save(ProjectEntity project) {
        ProjectTable projectRow = projectDatabaseClient.save(ProjectTable.builder()
                .id(project.getId())
                .createdAt(project.getCreatedAt())
                .updatedAt(project.getUpdatedAt())
                .name(project.getName())
                .description(project.getDescription())
                .userId(project.getUserId())
                .status(project.getStatus())
                .build());

        Iterable<ProjectFileTable> exitedFiles = projectfileDatabaseClient.findByFileId(projectRow.getId());
        List<Long> existedFileIds = StreamSupport.stream(exitedFiles.spliterator(), false)
                .map(ProjectFileTable::getId)
                .toList();

        List<ProjectFileTable> projectFileRows = project.getFiles()
                .stream()
                .filter(file -> !existedFileIds.contains(file.getId()))
                .map(file -> ProjectFileTable.builder()
                        .createdAt(file.getCreatedAt())
                        .updatedAt(file.getUpdatedAt())
                        .projectId(projectRow.getId())
                        .fileId(file.getId())
                        .build()
                )
                .toList();

        projectfileDatabaseClient.saveAll(projectFileRows);

        return ProjectEntity.builder()
                .id(projectRow.getId())
                .createdAt(projectRow.getCreatedAt())
                .updatedAt(projectRow.getUpdatedAt())
                .name(projectRow.getName())
                .description(projectRow.getDescription())
                .userId(projectRow.getUserId())
                .status(projectRow.getStatus())
                .files(project.getFiles())
                .build();
    }

    public List<ProjectEntity> findAll(List<ProjectStatus> projectStatuses) {
        List<ProjectTable> rows = projectDatabaseClient.findAllByStatusIn(projectStatuses);

        return rows.stream()
                .map(
                        it -> ProjectEntity.builder()
                                .id(it.getId())
                                .createdAt(it.getCreatedAt())
                                .updatedAt(it.getUpdatedAt())
                                .name(it.getName())
                                .description(it.getDescription())
                                .userId(it.getUserId())
                                .status(it.getStatus())
                                .build()
                )
                .toList();
    }
}
